package com.lw.controller;

import com.lw.common.IJsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/users")
public class UserController {

    //Get /users/login.action
    @GetMapping("/login")
    public String login(){
        return "login"; //到 login.jsp
    }

    //Post /users/login.action
    @PostMapping("login")
    @ResponseBody //返回Json数据
    public IJsonResult userLogin(String username, String password, HttpServletRequest request, HttpServletResponse response){
        //查询数据库获取是否是正常用户

        //无状态的session
        request.getSession().setAttribute("sessionUser","userInfo");
        return IJsonResult.ok();
    }

    @PostMapping("logout")
    @ResponseBody
    public IJsonResult userLogout(int userId,HttpServletRequest request){
        request.getSession().removeAttribute("sessionUser");
        return IJsonResult.ok();
    }

}
